// pages/games/rocket/entry/entry.js
var app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data:[],
    timu_num:0,
    index:0,//当前题目下标
    status: 'error',
    type:1, //1为选选选 2为火箭发射
    jieguo:[], //答题结果
    uid:0,
    game_type:0,
    is_start:0 //是否点击开始
  },
  //选择答案
  dati:function(e)
  {
    
    var that=this
  
    //判断是否是最后一题
     console.log(e)
     var timu_id=e.currentTarget.dataset.id
     var timu_xuanxiang=e.currentTarget.dataset.xuanxiang //1选项一 2选项二
    var jieguo=that.data.jieguo
    //判断是否回答正确

    for (let o = 0; o < that.data.data.length; o++) {
       
      if(timu_id==that.data.data[o]['id'] && timu_xuanxiang!= that.data.data[o]['daan']){
        console.log("回答错误")
            wx.showToast({
              title: '错误,正确答案->选项'+that.data.data[o]['daan'],
              icon:"none"
            })
      }
     
    }



    var item={id:timu_id,xuanxiang:timu_xuanxiang}
    var a=0;
    for(var i=0;i<jieguo.length;i++)
    {
      var id=jieguo[0]['id']

      if(id==timu_id)
      {
         jieguo[i]=item
      }else{
        a++;
      }
    }
   if(a==jieguo.length)
   {
     jieguo.push(item)
   }


      that.setData({
    index:parseInt(that.data.index+1)
     })
        console.log(that.data.jieguo)

    //如果到达最后一题结束游戏
    if(that.data.jieguo.length==that.data.data.length)
    {
      console.log("游戏结束")
      that.setData({
        is_start:0
      })
      that.addJieguo(that.data.jieguo)

    }else{
      console.log("游戏继续")
    }
  },
  //结束游戏提交数据
 addJieguo:function(jieguo)
 {
  var that=this
  wx.request({
    url: app.data.service_url+'game/addJieguo', //改成你服务端的方法
    data: {
      uid:that.data.uid,
      jieguo:JSON.stringify(that.data.jieguo),
      game_type:that.data.game_type,
      type:1 //选选
    },
    method:"POST",
    header:{
      "Content-Type": "application/x-www-form-urlencoded"
       },  
    success: function(res) {
      console.log(res)
      var code=res.data['code']
      if(code==1)
      {
      var jieguo_id=res.data['jieguo_id']
      wx.showToast({
        title: '挑战结束',
        icon:"none",
        success(res)
        {
          wx.navigateTo({
            url: '/pages/games/xuanze/res?jieguo_id='+jieguo_id,
          })
        }
      })

      }else{
        wx.showToast({
          title: '网络错误',
          icon:"none",
          success()
          {
            wx.switchTab({
              url: '/pages/tabs/home/home',
            })
          }
        })
      }


    }
  })

 },


    /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
     var that=this
     wx.getStorage({
       key:"uid",
       success(res)
       {
         that.setData({
           uid:res.data
         })
       }
     })

  },
  
  //随机获取获取题目 10条
  getTimu:function(game_type)
  {
    var that=this
    wx.request({
      url: app.data.service_url+'game/getTimu', //改成你服务端的方法
      data: {
        type:1,
        game_type:game_type
      },
      method:"POST",
      header:{
        "Content-Type": "application/x-www-form-urlencoded"
         },  
      success: function(res) {
        console.log(res)
        if(res.data.length==0)
        {
          wx.showToast({
            title: '没有题目',
            icon:"error",
            success()
            {
              wx.switchTab({
                url: '/pages/tabs/home/home',
              })
            }
          })
          return false
        }
        that.setData({
          data:res.data,
          timu_num:res.data.length
        })

      }
    })




  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var that=this
      var game_type=options.game_type
      that.getTimu(game_type)
      that.setData({
        game_type:game_type
      })

  },
  back:function()
  {
   wx.navigateBack({
     delta: 0,
   })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  handle(){
    console.log("游戏开始")
     var that=this
      
     that.setData({
       is_start:1
     })
 
   },
  next(){

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})